﻿#include "widget.h"
#include "ui_widget.h"
#include <QDebug>



Point point1, point2; /* vertical points of the bounding box */
int drag = 0;
Rect rect; /* bounding box */
Mat img, roiImg; /* roiImg - the part of the image in the bounding box */
int select_flag = 0;

int final_x=0;
int final_y=0;
int final_width=0;
int final_height=0;

void mouseHandler(int event, int x, int y, int flags, void* param)
{
    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        /* left button clicked. ROI selection begins */
        point1 = Point(x, y);
        drag = 1;
        qDebug()<<"button down";
    }

    if (event == CV_EVENT_MOUSEMOVE && drag)
    {
        /* mouse dragged. ROI being selected */
        qDebug()<<"moving......";
        Mat img1 = img.clone();
        point2 = Point(x, y);
        rectangle(img1, point1, point2, CV_RGB(255, 0, 0), 1, 8, 0);
        imshow("image", img1);
    }

    if (event == CV_EVENT_LBUTTONUP && drag)
    {
        qDebug()<<"up up up";
        point2 = Point(x, y);
        if(point2.x < point1.x){
            rect = Rect(point2.x,point2 .y,abs(x-point1.x),abs(y-point1.y));
        }else{
            rect = Rect(point1.x,point1.y,abs(x-point1.x),abs(y-point1.y));
        }
        drag = 0;
        roiImg = img(rect);
    }

    if (event == CV_EVENT_LBUTTONUP)
    {
       /* ROI selected */
        qDebug()<<"up.....";
        select_flag = 1;
        drag = 0;
        qDebug()<<"rect: "<<rect.x<<"<---x y--->"<<rect.y<<"   "<<rect.width<<"<---width height--->"<<rect.height;
        final_x=rect.x;
        final_y=rect.y;
        final_width=rect.width;
        final_height=rect.height;
    }
}



Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setAcceptDrops (true);
    connect (this,SIGNAL(readyimage(const QString & )),this,SLOT(getRectInfo(const QString &)));
}

Widget::~Widget()
{
    delete ui;
    cvDestroyWindow("image");
}

void Widget::getRectInfo(const QString &imageName)
{

    img = imread (imageName.toStdString ().c_str ());
    namedWindow ("image");
    cvSetMouseCallback("image", mouseHandler, NULL);
    imshow("image", img);
    waitKey();
    setMouseCallback("image", NULL, NULL);
    cvDestroyWindow("image");

}

void Widget::on_pushButton_clicked()
{
    QString RectInfo = QString("[%1 %2 %3 %4]").arg(QString::number (final_x),QString::number (final_y),QString::number (final_width),QString::number (final_height));
    ui->textEdit->clear ();
    ui->textEdit->append ("[x y width height]\n");
    ui->textEdit->append (RectInfo);

}

void Widget::dragEnterEvent(QDragEnterEvent *e)
{
    if (e->mimeData()->hasUrls()) {
        e->acceptProposedAction();
    }
}

void Widget::dropEvent(QDropEvent *e)
{
    QPoint p= e->pos();
    QPoint lp= ui->label->pos();
    QRect r(lp.x(),lp.y(),ui->label->width(),ui->label->height());
    if(r.contains(p)){
        foreach (const QUrl &url, e->mimeData()->urls()) {
            const QString &fileName = url.toLocalFile();
            ui->textEdit->clear ();
            ui->textEdit->append (fileName);
            emit readyimage(fileName);

        }
    }
}
